{% extends 'main_frame.html' %}

{% block content %}
{% include "search.html" %}
<table class="layui-hide" id="LAY_table" lay-filter="LAY_table"></table>
<script type="text/html" id="LAY_bar">
    <a class="layui-btn layui-btn-xs" lay-event="view" title="">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit" title="">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete" title="">删除</a>
</script>
<div class="layui-bar-buttons">
    <span id="add_goods">添加</span>
</div>
{% endblock %}


{% block js %}
<script>

    var cols=[[

        {type: 'numbers',title: '序号',width: 60},
        {field:'goodsId',title: '商品id',width: 150},
        {field:'goodsName',title: '商品名称',width: 150},
        {field:'shopPrice',title: '商品价格',width: 150},
        {field:'goodsSn',title: '商品编号',width: 150},
        {field:'brandName',title: '品牌',width: 150},
        {field:'storeCount',title: '库存',width: 150},
        {field:'goodsRemark',title: '简述',width: 150},
        {field:'goodsContent',title: '详情',width: 150},
        {field:'isOnSale',title: '上架',width: 150},
        {field:'onTime',title: '上架时间',width: 150},
        {field:'btns',title: '操作',align: 'center',toolbar: '#LAY_bar',event:'click',fixed: 'right',width:160}
    ]]

    //监听工具栏
    layui.table.on('tool(LAY_table)', function(obj) {
        var data = obj.data;
        switch(obj.event){
            //查看
            case 'view' :
                Methods.view(data);
                break;
            //修改
            case 'edit' :
                Methods.info(data);
                break;
            //删除
            case 'delete' :
                Methods.delete(data);
                break;

            default:
                break;
        }
    });
    //方法集合
    var Methods={
        render:function(where){
            layui.table.render({
                elem: '#LAY_table',
                method:'POST',
                contentType:'application/json',
                url:API.goods.list,
                where:where,
                cols: cols,
                id: 'LAY_table',
                page: true,
                limits: [10, 15, 20, 30, 40, 50],
                loading: true,
                height: FX.getWinHeight(74),
                skin: '', //行边框风格
                even: true, //开启隔行背景
                size: 'big', //小尺寸的表格
                done:function(){

                }
            });
        },
        view:function(data){
            layer.open({
                type: 1,
                title:'信息',
                btn: ['关闭'],
                area:['640px','400px'],
                shadeClose:true,
                anim:5,
                content:function(){
                    return FX.getView({
                        '商品名称':data.goodsName,
                        '价格':data.shopPrice,
                        '商品编号':data.goodsSn,
                        '品牌':data.brandName,
                        '商品库存':data.storeCount,
                        '商品简介':data.goodsRemark,
                        '商品图片':data.originalImg,
                        '商品详情':data.goodsContent,
                        '是否上架':data.isOnSale,
                        '上架时间':data.onTime
                    })
                }(),
                yes:function(index, layero){
                    layer.close(index);
                },
                end:function(){

                },
                success:function(layero,index){

                }
            });
        },
        info:function(obj){
            FX.ajax({
                url:API.goods.info + obj.goodsId,
                success:function(res){
                    if(res.code==0){
                        Methods.edit(res.data)
                    }
                }
            })
        },
        edit:function(obj){
            layer.open({
                type: 1,
                title:'信息',
                btn: ['保存'],
                area:['640px','400px'],
                shadeClose:true,
                anim:5,
                content:function(){
                    return renderForm([
                        { title:{name:'商品id'},content:[{type:'hidden',name:'goodsId',disabled:true,value:obj.goodsId}]},
                        { title:{name:'商品名称'}, content:[{type:'input',name:'goodsName',verify:true,value:obj.goodsName}]},
                        { title:{name:'商品价格'}, content:[{type:'input',name:'shopPrice',verify:true,value:obj.shopPrice}]},
                        { title:{name:'商品编号'}, content:[{type:'input',name:'goodsSn',disabled:true,value:obj.goodsSn}]},
                        { title:{name:'品牌'}, content:[{type:'input',name:'brandName',value:obj.brandName}]},
                        { title:{name:'库存'}, content:[{type:'input',name:'storeCount',value:obj.storeCount}]},
                        { title:{name:'简述'}, content:[{type:'input',name:'goodsRemark',value:obj.goodsRemark}]},
                        { title:{name:'详情'}, content:[{type:'input',name:'goodsContent',value:obj.goodsContent}]},
                        { title:{name:'分类'}, content:[{type:'input',name:'categoryId',value:obj.categoryId}]},
                        { title:{name:'上架'}, content:[{type:'input',name:'isOnSale',value:obj.isOnSale}]}
                    ])

                }(),
                yes:function(index, layero){
                    layui.form.on('submit(lay-submit)', function(obj){
                        FX.ajax({
                            url:API.goods.edit,
                            type:'post',
                            data:obj.field,
                            success:function(res){
                                FX.success('修改成功');
                                layer.close(index);
                            }
                        })
                        return false;
                    });
                    $("#lay_submit_btn").click();
                },
                end:function(){

                },
                success:function(layero,index){
                    layui.form.render('select');
                    FX.initDate();
                }
            })
        },
        add:function(data){
            layer.open({
                type: 1,
                title:'信息',
                btn: ['保存'],
                area:['640px','400px'],
                shadeClose:true,
                anim:5,
                content:function(){
                    return renderForm([
                        // { title:{name:'用户名'}, content:[{type:'input',name:'userName',verify:true}]},
                        // { title:{name:'密码'}, content:[{type:'password',name:'password',verify:true}]},
                        // { title:{name:'手机号'}, content:[{type:'input',name:'mobile',verify:true}]},
                        // { title:{name:'邮箱'}, content:[{type:'input',name:'email'}]},
                        // { title:{name:'真实姓名'}, content:[{type:'input',name:'realName'}]},
                        // { title:{name:'昵称'}, content:[{type:'input',name:'nickName'}]},
                        // { title:{name:'性别'}, content:[{type:'select',name:'sex',options:[{text:'男',value:1},{text:'女',value:2}]}]},
                        // { title:{name:'生日'}, content:[{type:'date',name:'birthday'}]},
                        // { title:{name:'收货地址'}, content:[{type:'input',name:'addressId'}]},
                        // { title:{name:'头像'}, content:[{type:'input',name:'headPic'}]}
                    ])

                }(),
                yes:function(index, layero){
                    layui.form.on('submit(lay-submit)', function(obj){
                        FX.ajax({
                            url:API.goods.add,
                            type:'post',
                            data:obj.field,
                            success:function(res){
                                FX.success('添加成功');
                                layer.close(index);
                            }
                        })
                        return false;
                    });
                    $("#lay_submit_btn").click();
                },
                end:function(){

                },
                success:function(layero,index){
                    layui.form.render('select');
                    FX.initDate();
                }
            });
        },
        delete:function(obj){
            layer.confirm('是否删除',function(index){
                layer.close(index);
                FX.ajax({
                    url:API.user.delete + obj.userId,
                    success:function(res){
                        if(res.code==0){
                            FX.success('删除成功');
                        }
                    }
                })

            })
        }
    }
    $("#add_goods").click(function(){
        Methods.add();
    })


    Methods.render();
    FX.getSearch(WHERE.goodsList,Methods.render);

</script>
{% endblock %}